package 对象的比较;

import javafx.animation.Interpolatable;
import java.util.*;

class Student implements Comparable<Student> {
    int age;
    String name;

    public Student( String name,int age) {
        this.age = age;
        this.name = name;
    }

    @Override
    public int compareTo(Student o) {
        return this.age-o.age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }
}
class IntCmp implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        /**
         * 小根堆
         * return o1.age-o2.age;
         */
        //大根堆
        return o2.age-o1.age;

    }

}
class StrCmp implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        //小根堆
//        return o1.name.compareTo(o2.name);
        //大根堆
        return o2.name.compareTo(o1.name);
    }
}
public class Test {
    public static void main(String[] args) {
        Queue<Student>queue=new PriorityQueue<Student>(new StrCmp());
        System.out.println(new Student("张三",66).equals(new Student("张三",66)));
        queue.offer(new Student("a",8));
        queue.offer(new Student("b",12));
        queue.offer(new Student("z",66));
        System.out.println(queue.peek().name);
        System.out.println("=========");
        Queue<Student>queue1=new PriorityQueue<Student>(new IntCmp());
        queue1.offer(new Student("a",8));
        queue1.offer(new Student("b",12));
        queue1.offer(new Student("z",66));
        System.out.println(queue1.peek().age);
    }
    public static void main2(String[] args) {
        int []array={11,23,33,44,56,6,4,48};
        Test test=new Test();
        int[]ret=test.MaxK(array,3);
        System.out.println(Arrays.toString(ret));
        int[]ret1=test.smallestK(array,3);
        System.out.println(Arrays.toString(ret1));
    }
    public static void main1(String[] args) {
        Queue<Student> deque=new PriorityQueue<>();
        deque.offer(new Student("李四",18));
        deque.offer(new Student("张三",15));
        System.out.println("=========");
        System.out.println(Objects.requireNonNull(deque.poll()).age);
        System.out.println(Objects.requireNonNull(deque.poll()).age);
        System.out.println("=========");
        Queue<Student>queue=new PriorityQueue<>(new IntCmp());
        queue.offer(new Student("a",22));
        queue.offer(new Student("y",19));
        queue.offer(new Student("c",18));
        System.out.println(queue.poll().age);
        System.out.println(queue.poll().age);
        System.out.println(queue.poll().age);
    }
    public int[] smallestK1(int[] arr, int k) {
    //最小k歌数
        //建立大根堆，弹k次
        int[]array=new int[k];
        Queue<Integer>queue=new PriorityQueue<>();
        for (int i = 0; i < arr.length; i++) {
            queue.offer(arr[i]);
        }
        for (int i = 0; i < k; i++) {
            array[i]=queue.poll();
        }
        return array;
    }

    public int[] smallestK(int[] arr, int k) {
    //
        int[]ret =new int[k];
        Queue<Integer> queue=new PriorityQueue<>(new Cmp());

        for (int i = 0; i < k; i++) {
            queue.offer(arr[i]);
        }
        for (int i = k; i < arr.length; i++) {
            if(queue.peek()>arr[i]) {
                queue.poll();
                queue.offer(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i]=queue.poll();
        }
        return ret;
    }

    public int[] MaxK(int[] arr, int k) {
        //
        int[]ret =new int[k];
        Queue<Integer> queue=new PriorityQueue<>(k);
        for (int i = 0; i < k; i++) {
            queue.offer(arr[i]);
        }
        for (int i = k; i < arr.length; i++) {
            if(queue.peek()< arr[i]) {
                queue.poll();
                queue.offer(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i]=queue.poll();
        }
        return ret;
    }
}

class Cmp implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2-o1;
    }
}